package user;
import java.io.DataOutput;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;

import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;

import common.DBUtil;
import news.News;
/**
 * 用户业务处理类(功能类)
 * @author 谢一弘
 * @param <QueryRunner>
 * @date 2020-4-14
 */
public class UserDao<QueryRunner> {
	private User user;// 用户信息
	private DBUtil db = new DBUtil();

	public UserDao() {

	}
	public  UserDao(User user) {
       this.user= user;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	/**
	 * 注册
	 * @param user  用户信息(用户名、密码等注册信息)
	 * @return >=0 -成功 -1-失败
	 */
	public int doRegister(User user) {
		db = new DBUtil();
		int result = -1;
		String sql = "insert into user(uname,pass,sex,profession,favourite,note)";
		sql += "values('" + user.getUname() + "','" + user.getPass() + "','" + user.getSex() + "','"
				+ user.getProfession() + "','" + user.getFavourite() + "','" + user.getNote() + "')";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
    /**
     * 注册
     * @return -1-失败 >=0 成功
     */
	public int doRegister() {
		db = new DBUtil();
		int result = -1;
		String sql = "insert into user(uname,pass,sex,profession,favourite,note)";
		sql += "values('" + user.getUname() + "','" + user.getPass() + "','" + user.getSex() + "','"
				+ user.getProfession() + "','" + user.getFavourite() + "','" + user.getNote() + "')";
		result = db.executeUpdate(sql);
		return result;
	}
	
	/**
	 * 登录验证
	 * @param uname  用户名
	 * @param pass  密码
	 * @return true-成功 false-失败
	 */
	public boolean isLogin(String uname, String pass) {
		db = new DBUtil();
		boolean result = false;
		String sql = "select uname from user where uname='" + uname + "'and pass='" + pass + "';";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			result = resultSet.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (resultSet != null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		db.closeDB();
		return result;
	}
	/**
	 * 登录验证
	 * @return true-成功 false-失败
	 */
	public boolean isLogin() {
		db = new DBUtil();
		boolean result = false;
		String sql = "select uname from user where uname='" + user.getUname() + "'and pass='" + user.getPass() + "'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			result = resultSet.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (resultSet != null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		db.closeDB();
		return result;
	}
	/**
	 * 登录验证
	 * @param user
	 * @return true-成功 false-失败
	 */
	public boolean isLogin(User user) {
		db = new DBUtil();
		boolean result = false;
		String sql = "select uname from user where uname='" + user.getUname() + "'and pass='" + user.getPass() + "'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			result = resultSet.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (resultSet != null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		db.closeDB();
		return result;
	}
	/**
	 * 判断用户名是否存在
	 * @param uname 用户名
	 * @return true-成功 false-失败
	 */
	public boolean isExistsUname(String uname) {
		db = new DBUtil();
		boolean result = true;
		String sql = "select uname from user where uname='" + uname + "'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			result = resultSet.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (resultSet != null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return result;
	}
	/**
	 * 判断用户名是否存在
	 * @param user
	 * @return true-成功 false-失败
	 */
	public boolean isExistsUname(User user) {
		db = new DBUtil();
		boolean result = true;
		String sql = "select uname from user where uname='" + user.getUname() + "'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			result = resultSet.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (resultSet != null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		db.closeDB();
		return result;
	}
	/**
	 * 判断用户名是否存在
	 * @return true-成功 false-失败
	 */
	public boolean isExistsUname() {
		db = new DBUtil();
		boolean result = true;
		String sql = "select uname from user where uname='" + user.getUname() + "'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			result = resultSet.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (resultSet != null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		db.closeDB();
		return result;
	}
	/**
	 * 重置密码
	 * @param uid
	 * @return >=0 -成功 -1-失败
	 */
	public int doResetPassword() {
		db = new DBUtil();
		int result = -1;
		String sql = "update user set pass ='123456' where uid =" + user.getUid() + ";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
   /**
    * 重置密码
    * @param uname 用户名
    * @return -1-失败 >=0 成功
    */
	public int doResetPassword(String uname) {
		db = new DBUtil();
		int result = -1;
		String sql = "update user set pass ='123456' where uid =" + uname + ";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
   /**
    * 重置密码
    * @param user 用户
    * @return -1-失败 >=0 成功
    */
	public int doResetPassword(User user) {
		db = new DBUtil();
		int result = -1;
		String sql = "update user set pass ='123456' where uid =" + user.getUid() + ";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 重重密码
	 * @param uid 用户编号
	 * @return >=0 -成功 -1-失败
	 */
	public int doResetPassword(int uid) {
		db = new DBUtil();
		int result = -1;
		String pass = string2MD5("123456");
		String sql = "update user set pass ='"+pass+"' where uid =" + uid + ";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 修改个人密码
	 * @param uname 用户名
	 * @param newPassword 新密码
	 * @return -1-失败 >=0 成功
	 */
	public int doUpdatePassword(String uname, String newPassword) {
		db = new DBUtil();
		int result = -1;
		String sql = "update user set pass ='" + newPassword + "' where uname =" + uname + ";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;

	}
  /**
   * 修改密码
   * @param user 用户
   * @param newPassword 新密码
   * @return -1-失败 >=0 成功
   */
	public int doUpdatePassword(User user, String newPassword) {
		db = new DBUtil();
		int result = -1;
		String sql = "update user set pass =" + newPassword + "  where uid =" + user.getUid() + ";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
    /**
     * 修改密码
     * @param uid 用户编号
     * @param newPassword 新密码
     * @return -1-失败 >=0 成功
     */
	public int doUpdatePassword(int uid, String newPassword) {
		db = new DBUtil();
		int result = -1;
		String sql = "update user set pass ='"+newPassword+"'  where uid ="+uid+";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 根据用户名得到完整的用户信息
	 * @param uname
	 * @return 用户对象(即完整的用户信息)
	 */
	public User getUserByUname(String uname) {
		db = new DBUtil();
		User user = null;
		String sql = "select uid,uname,pass,sex,profession,favourite,note,type,isValid from user where uname='" + uname+"'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			if (resultSet.next()) {
				user = new User();
				user.setUid(resultSet.getInt("uid"));
				user.setUname(resultSet.getString("uname"));
				user.setPass(resultSet.getString("pass"));
				user.setSex(resultSet.getString("sex"));
				user.setProfession(resultSet.getString("profession"));
				user.setFavourite(resultSet.getString("favourite"));
				user.setNote(resultSet.getString("note"));
				user.setType(resultSet.getInt("type"));
				user.setIsValid(resultSet.getInt("isValid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	/**
	 *  根据用户编号得到用户的完整信息
	 * @param uid
	 * @return user
	 */
	public User doSelectById(int uid) {
		db = new DBUtil();
		User user = null;
		String sql = "select uid,uname,pass,sex,profession,favourite,note,type,isValid from user where uid='" + uid+"'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			if (resultSet.next()) {
				user = new User();
				user.setUid(resultSet.getInt("uid"));
				user.setUname(resultSet.getString("uname"));
				user.setPass(resultSet.getString("pass"));
				user.setSex(resultSet.getString("sex"));
				user.setProfession(resultSet.getString("profession"));
				user.setFavourite(resultSet.getString("favourite"));
				user.setNote(resultSet.getString("note"));
				user.setType(resultSet.getInt("type"));
				user.setIsValid(resultSet.getInt("isValid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
		
	}
	/**
	 * 显示用户信息
	 * @param uname 用户名
	 * @return 用户列表
	 */
	 public ArrayList<User> doSelectByUname(String uname) {
		 db = new DBUtil();
		ArrayList<User> list = new ArrayList<User>();
		String sql = "select uid,uname,pass,sex,profession,favourite,note,type,isValid from user where uname like '%"+ uname + "%'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			while (resultSet.next()) {
				User user = new User();
				user.setUid(resultSet.getInt("uid"));
				user.setUname(resultSet.getString("uname"));
				user.setPass(resultSet.getString("pass"));
				user.setSex(resultSet.getString("sex"));
				user.setProfession(resultSet.getString("profession"));
				user.setFavourite(resultSet.getString("favourite"));
				user.setType(resultSet.getInt("type"));
				user.setIsValid(resultSet.getInt("isValid"));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeDB();
		return list;
	}
		/**
		 * 显示用户信息
		 * @param uname 用户名
		 * @return 用户列表
		 */
		 public ArrayList<User> doSelectByID(int uid) {
			 db = new DBUtil();
			ArrayList<User> list = new ArrayList<User>();
			String sql = "select uid,uname,pass,sex,profession,favourite,note,type,isValid from user where uid="+uid+"";
			ResultSet resultSet = db.executeQuery(sql);
			try {
				while (resultSet.next()) {
					User user = new User();
					user.setUid(resultSet.getInt("uid"));
					user.setUname(resultSet.getString("uname"));
					user.setPass(resultSet.getString("pass"));
					user.setSex(resultSet.getString("sex"));
					user.setProfession(resultSet.getString("profession"));
					user.setFavourite(resultSet.getString("favourite"));
					user.setType(resultSet.getInt("type"));
					user.setIsValid(resultSet.getInt("isValid"));
					user.setNote(resultSet.getString("note"));
					list.add(user);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			db.closeDB();
			return list;
		}
	/**
	 * 显示全部用户的信息
	 * @return 用户列表
	 */
	public ArrayList<User> doSelectByUname() {
		db = new DBUtil();
		ArrayList<User> list = new ArrayList<User>();
		String sql = "select uid,uname,pass,sex,profession,favourite,note,type,isValid from user";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			while (resultSet.next()) {
				User user = new User();
				user.setUid(resultSet.getInt("uid"));
				user.setUname(resultSet.getString("uname"));
				user.setPass(resultSet.getString("pass"));
				user.setSex(resultSet.getString("sex"));
				user.setProfession(resultSet.getString("profession"));
				user.setFavourite(resultSet.getString("favourite"));
				user.setType(resultSet.getInt("type"));
				user.setIsValid(resultSet.getInt("isValid"));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(resultSet != null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		db.closeDB();
	    return list;
	}
	/**
	 * 修改个人信息
	 * @param user(用户名，用户密码)
	 * @return >=0 -成功 -1-失败
	 */
	public int doUpdateUser(User user) {
		db = new DBUtil();
		int result = -1;
		String sql = "update user set sex ='"+user.getSex()+"',profession='"+user.getProfession()+"',favourite='"+user.getFavourite()+"',note='"+user.getNote()+"' where uid = '"+user.getUid()+"';";
		result = db.executeUpdate(sql);
		return result;
	}
	/**
	 * 修改用户的状态
	 * @param uid  用编号
	 * @return >=0 -成功 -1-失败
	 */
	public int doChangeState(int uid) {
		db = new DBUtil();
		int isValid = 0;
		int isValid_1;
		String sql = "select isValid from user where uid='" + uid + "'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			if (resultSet.next()) {
				isValid = resultSet.getInt("isValid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//如果该用户为有效，那么更改后为无效
		if (isValid == 0) {
			isValid_1 = 1;
		} else {
			isValid_1 = 0;
		}
		// System.out.println(isValid_1);
		String sql_1 = "update user set isValid  =" + isValid_1 + "  where uid ='" + uid + "';";
		int result = db.executeUpdate(sql_1);
		return result;
	}
	/**
	 * 修改用户的类型
	 * @param uid 用户编号
	 * @return >=0 -成功 -1-失败
	 */
	public int doChangePower(int uid) {
		db = new DBUtil();
		int type = 0;
		int type_1;
		String sql = "select type from user where uid='" + uid + "'";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			if (resultSet.next()) {
				type = resultSet.getInt("type");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//如果该用户是普通用户，更改后为管理员
		if (type == 0) {
			type_1 = 1;
		} else {
			type_1 = 0;
		}
		// System.out.println(type_1);
		String sql_1 = "update user set type  =" + type_1 + "  where uid ='" + uid + "';";
		int result = db.executeUpdate(sql_1);
		return result;
	}
	/**
	 * 删除用户
	 * @param uid 用户编号
	 * @return -1-失败 >=0 成功
	 */
	public int doDeleteUserByID(int uid) {
		DBUtil db = new DBUtil();
		
		int result = -1;
		String sql = "delete from user where uid=" + uid + "";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 删除用户
	 * @param uname  用户姓名
	 * @return -1-失败 >=0 成功
	 */
	public int doDeleteUserByID(String uname) {
		int result = -1;
		String sql = "delete from user where uname=" + uname + "";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 删除用户
	 * @param user
	 * @return -1-失败 >=0 成功
	 */
	public int doDeleteUserByID(User user) {
		db = new DBUtil();
		int result = -1;
		String sql = "delete from user where uid=" + user.getUid() + "";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 删除用户
	 * @return -1-失败 >=0 成功
	 */
	public int doDeleteUserByID() {
		db = new DBUtil();
		int result = -1;
		String sql = "delete from user where uid=" + user.getUid() + "";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * MD5加密 生成64为的md5码
	 * @param pass
	 * @return
	 */
	public String string2MD5(String pass) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
		    pass = new BigInteger(1,md5.digest(pass.getBytes())).toString(64);
		    pass = new String(pass.getBytes());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return pass;
		
	}
	/**
	 * 加密解密算法执行一次加密，两次解密
	 * @param pass
	 * @return
	 */
	public String convertMD5(String pass) {
		char [] a=pass.toCharArray();
		for(int i =0; i< a.length;i++) {
			a[i] = (char)(a[i]^'t');
		}
		String string = new String(a);
		return string;
		
	}
	public ArrayList<User> doUserPage(int pages , int limit) {
		ArrayList<User> list = new ArrayList<User>();
		db = new DBUtil();
		String sql = "select * from user order by uid limit " + (pages - 1) * limit + "," + limit+";";
		ResultSet resultSet = db.executeQuery(sql);
		try {
			while(resultSet.next()) {
				  user = new User();
				  user.setUid(resultSet.getInt("uid"));
				  user.setUname(resultSet.getString("uname"));
				  user.setSex(resultSet.getString("sex"));
				  user.setProfession(resultSet.getString("profession"));
				  user.setFavourite(resultSet.getString("favourite"));
				  user.setType(resultSet.getInt("type"));
				  user.setIsValid(resultSet.getInt("isValid"));
	              list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		}
}
